package com.dm.wallpaper.library.ablility;

import com.dm.wallpaper.library.ResourceTable;
import com.dm.wallpaper.library.fraction.*;
import com.dm.wallpaper.library.ablility.slice.WallpaperBoardAbilitySlice;
import com.dm.wallpaper.library.utils.Log;
import com.dm.wallpaper.library.utils.SystemUtils;
import com.dm.wallpaper.library.utils.Toast;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;
import ohos.utils.PacMap;


public class WallpaperAbility extends FractionAbility implements Component.ClickedListener {
    PopupDialog munePop;
    private static int showTag = ResourceTable.Id_wallpapers;
    boolean needShowPop;



    public static int getShowTag() {
        return showTag;
    }

    public static void setShowTag(int showTag) {
        WallpaperAbility.showTag = showTag;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(WallpaperBoardAbilitySlice.class.getName());
    }

    @Override
    public void attachBaseContext(Context base) {
        Log.i("WallpaperAbility.attachBaseContext([base]):32 ");
        super.attachBaseContext(base);
    }

    @Override
    protected void onActive() {
        Log.i("WallpaperAbility.onActive([]):45");
        super.onActive();
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        Log.i("WallpaperAbility.onSaveAbilityState([outState]):56  ");
        super.onSaveAbilityState(outState);
    }

    @Override
    protected void onForeground(Intent intent) {
        Log.i("WallpaperAbility.onForeground([intent]):62");
        super.onForeground(intent);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        Log.i("WallpaperAbility.onRestoreAbilityState([inState]):67   10:50:52.508   10:50:52.495");
        super.onRestoreAbilityState(inState);

    }

    public void showMunePop() {
        Component pop_itemlayout = LayoutScatter.getInstance(getApplicationContext()).parse(ResourceTable.Layout_mune_layout, null, true);
        initView(pop_itemlayout);
        ComponentContainer.LayoutConfig layoutConfig = pop_itemlayout.getLayoutConfig();
        layoutConfig.height = SystemUtils.getDisplayHightInPx(getContext());
        pop_itemlayout.setLayoutConfig(layoutConfig);
        munePop = new PopupDialog(getContext(), pop_itemlayout);
        munePop.setCustomComponent(pop_itemlayout);
        munePop.setAutoClosable(true);
        setViewSelect(pop_itemlayout);
        munePop.showOnCertainPosition(LayoutAlignment.LEFT | LayoutAlignment.TOP, 0, 0);
    }

    @Override
    protected void onBackground() {
        Log.i("WallpaperAbility.onBackground([]):68");
        super.onBackground();
        try {
            if (munePop != null && munePop.isShowing()) {
                munePop.hide();
                munePop.destroy();
                needShowPop = true;
            }
        } catch (Exception e) {
            Log.i("WallpaperAbility.onBackground([]):97       " + e.getMessage());
        }
    }

    private void setViewSelect(Component pop_itemlayout) {
        Color color = new Color(Color.getIntColor("#088A08"));
        if (showTag == ResourceTable.Id_favorites) {
            pop_itemlayout.findComponentById(ResourceTable.Id_favorite_Image);
            ((Text) pop_itemlayout.findComponentById(ResourceTable.Id_favorites_text)).setTextColor(color);
        } else if (showTag == ResourceTable.Id_about) {
            pop_itemlayout.findComponentById(ResourceTable.Id_about_image);
            ((Text) pop_itemlayout.findComponentById(ResourceTable.Id_about_text)).setTextColor(color);
        } else if (showTag == ResourceTable.Id_settings) {
            pop_itemlayout.findComponentById(ResourceTable.Id_settings_image);
            ((Text) pop_itemlayout.findComponentById(ResourceTable.Id_settings_text)).setTextColor(color);
        } else if (showTag == ResourceTable.Id_wallpapers) {
            pop_itemlayout.findComponentById(ResourceTable.Id_wallpapers_image);
            ((Text) pop_itemlayout.findComponentById(ResourceTable.Id_wallpapers_text)).setTextColor(color);
        } else if (showTag == ResourceTable.Id_muzei_setting) {
            pop_itemlayout.findComponentById(ResourceTable.Id_wallpapers_image);
            ((Text) pop_itemlayout.findComponentById(ResourceTable.Id_wallpapers_text)).setTextColor(color);
        }
    }


    public void initView(Component pop_itemlayout) {
        pop_itemlayout.findComponentById(ResourceTable.Id_favorites).setClickedListener(this);
        pop_itemlayout.findComponentById(ResourceTable.Id_about).setClickedListener(this);
        pop_itemlayout.findComponentById(ResourceTable.Id_settings).setClickedListener(this);
        pop_itemlayout.findComponentById(ResourceTable.Id_wallpapers).setClickedListener(this);
        pop_itemlayout.findComponentById(ResourceTable.Id_muzei_setting).setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        setShowTag(component.getId());
        munePop.hide();
        if (showTag == ResourceTable.Id_wallpapers) {
            FractionHelper.getInstance(this).showFraction(CollectionWallpaperFraction.class);
        } else if (showTag == ResourceTable.Id_favorites) {
            FractionHelper.getInstance(this).showFraction(FavoritesFraction.class);
        } else if (showTag == ResourceTable.Id_settings) {
            FractionHelper.getInstance(this).showFraction(SettingsFraction.class);
        } else if (showTag == ResourceTable.Id_about) {
            FractionHelper.getInstance(this).showFraction(AboutFraction.class);
        } else if (showTag == ResourceTable.Id_muzei_setting) {
            Intent getIntent = new Intent();
            Operation opt = new Intent
                    .OperationBuilder()
                    .withBundleName(getBundleName())
                    .withAbilityName(WallpaperBoardMuzeiMainAbility.class)
                    .build();
            getIntent.setOperation(opt);
            startAbility(getIntent);
//            Log.i("WallpaperAbility.onClick([component]):141          ");
//            "scheme": "virtual",
//                    "host": "main.com"

//            Intent getIntent = new Intent();
//            Operation opt = new Intent
//                    .OperationBuilder()
//                    .withUri(Uri.parse("virtual://main.com"))
//                    .build();
//            getIntent.setOperation(opt);
//            startAbility(getIntent);
//            Log.i("WallpaperAbility.onClick([component]):150            ");
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        for (int i = 0; i < permissions.length; i++) {
            Log.i("WallpaperAbility.onRequestPermissionsFromUserResult([requestCode, permissions, grantResults]):170     " + permissions[i] + "   " + grantResults[i]);
            if (permissions[i].equals("ohos.permission.WRITE_USER_STORAGE") && grantResults[i] == -1) {
                String promission = SystemUtils.getString(getContext(), ResourceTable.String_permission_storage_denied);
                Toast.show(this, promission, LayoutAlignment.BOTTOM);
            }
        }
    }
}
